package com.yyx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyx.server.mapper.EmployeeMapper;
import com.yyx.server.pojo.Department;
import com.yyx.server.mapper.DepartmentMapper;
import com.yyx.server.pojo.Employee;
import com.yyx.server.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyx.server.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YongXin
 * @since 2021-07-24
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {
        System.out.println(departmentMapper.getAllDepartments(-1));
        return departmentMapper.getAllDepartments(-1);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @Override
    @Transactional
    public ResultVo addDepartment(Department department) {
        department.setEnabled(true);
        department.setCreateDate(LocalDateTime.now());
        //添加部门的depPath
        Department parentDep = departmentMapper.selectById(department.getParentId());
        //department.setDepPath(parentDep.getDepPath().concat("."+department.getId()));
        department.setIsParent(false);
        //更新父部门isParent
        if (!parentDep.getIsParent()){
            parentDep.setIsParent(true);
            departmentMapper.updateById(parentDep);
        }
        if (departmentMapper.insert(department)==1){
            department.setDepPath(parentDep.getDepPath().concat("."+department.getId()));
            if(departmentMapper.updateById(department)==1){
                return ResultVo.success("添加成功");
            }
        }
        return ResultVo.error("添加失败");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResultVo delDepartment(Integer id) {
        Department department = departmentMapper.selectById(id);
        if (department.getIsParent()){
            return ResultVo.error("删除失败，存在子部门");
        }
        if (employeeMapper.selectCount(new QueryWrapper<Employee>().eq("department_id",id))!=0){
            return ResultVo.error("删除失败，该部门存在员工");
        }
        Integer parentId = department.getParentId();
        if (departmentMapper.deleteById(id)>0){
            if (departmentMapper.selectCount(new QueryWrapper<Department>().eq("parent_id",parentId))==0){
                Department parentDep = departmentMapper.selectById(parentId);
                parentDep.setIsParent(false);
                departmentMapper.updateById(parentDep);
            }
            return ResultVo.success("删除成功");
        }
        return ResultVo.error("删除失败");
    }
}
